package com.wl2o2o.leetcode;


import java.util.HashSet;

/**
 * @Author <a href="https://github.com/wl2o2o">程序员CSGUIDER</a>
 * @From <a href="https://wl2o2o.github.io">CSGUIDER博客</a>
 * @CreateTime 2024/4/20
 */

public class CycleList142PlanA {
    public ListNode detectCycle(ListNode head) {
        // base case
        if (head == null || head.next == null) {
            return null;
        }
        // 使用hashset
        HashSet<ListNode> set = new HashSet<>();
        while (head != null) {
            set.add(head);

            if (set.contains(head.next) && set.contains(head.next.next)) {
                return head.next;
            }
            head = head.next;
        }
        return null;
    }
}

